﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class BLLProducts
{
    DatabaseDataContext Context;

	public BLLProducts()
	{
        Context = new DatabaseDataContext();
	}
    public List<Product> GetProducts(string sortExp, string sortDirection, int pageIndex, int filterStatus, String filterSearch, int startRow,
        int pageSize, string sortColumns)
    {
        List<Product> list = new List<Product>();
        try
        {
            sortColumns = sortExp + " " + sortDirection;
            startRow = pageIndex * pageSize;
            var products = (from p in Context.Products select p);
            if (!String.IsNullOrEmpty(filterSearch))
            {
                products = products.Where(l => l.Name.Contains(filterSearch));
            }
            if (filterStatus > 0)
            {
                bool statusB = false;
                switch (filterStatus)
                {
                    case 1:
                        statusB = true;
                        break;
                    default:
                        statusB = false;
                        break;
                }
                products = products.Where(l => l.IsEnabled == statusB);
            }
            if (!String.IsNullOrEmpty(sortColumns))
            {
                if (sortColumns.Contains("DESC"))
                {
                    if (sortColumns.Contains("Name"))
                        products = products.OrderByDescending(l => l.Name);
                    if (sortColumns.Contains("isEnabled"))
                        products = products.OrderByDescending(l => l.IsEnabled);
                    if (sortColumns.Contains("Quantity"))
                        products = products.OrderByDescending(l => l.Quantity);
                }
                else
                {
                    if (sortColumns.Contains("Name"))
                        products = products.OrderBy(l => l.Name);
                    if (sortColumns.Contains("isEnabled"))
                        products = products.OrderBy(l => l.IsEnabled);
                    if (sortColumns.Contains("Quantity"))
                        products = products.OrderBy(l => l.Quantity);
                }
            }
            list = products.Skip(startRow).Take(pageSize).ToList();
        }
        catch
        {}
        if (list.Count == 0)
        {
            Product p = new Product();
            p.Id = -1;
            list.Add(p);
        }
        return list;
    }
    public int GetProductsCount(string sortExp, string sortDirection, int pageIndex, int filterStatus, String filterSearch)
    {
        int count = 0;

        try
        {
            var products = (from p in Context.Products select p);
            if (!String.IsNullOrEmpty(filterSearch))
            {
                products = products.Where(l => l.Name.Contains(filterSearch));
            }
            if (filterStatus > 0)
            {
                bool statusB = false;
                switch (filterStatus)
                {
                    case 1:
                        statusB = true;
                        break;
                    default:
                        statusB = false;
                        break;
                }
                products = products.Where(l => l.IsEnabled == statusB);
            }
            count = products.Count();
        }
        catch
        { }
        if (count == 0)
        {
            count = 1;
        }
        return count;
    }
    public bool UpdateProduct(Product pr)
    {
        bool result = false;
        try
        {
            Product product = (from p in Context.Products where p.Id == pr.Id select p).First();
            if (product != null)
            {
                product.Name = pr.Name;
                product.Description = pr.Description;
                product.IsEnabled = pr.IsEnabled;
                Context.SubmitChanges();
                result = true;
            }
        }
        catch
        {
        }
        return result;
    }
    public bool UpdateProductQuantity(Product p,bool flag)
    {
        bool result = false;
        try
        {
            Product product = (from prod in Context.Products where prod.Id == p.Id select prod).First();
            if (product.Id > 0)
            {
                if (product.Quantity == null)
                {
                    product.Quantity = 0;
                    product.Unit = p.Unit;
                }
                product.Quantity = (flag) ? (product.Quantity + p.Quantity) : (product.Quantity - p.Quantity) 
                    > 0 ? (product.Quantity - p.Quantity) : 0;
                Context.SubmitChanges();
                result = true;
            }
        }
        catch
        {
        }
        return result;
    }
    public Product GetProductById(int Id)
    {
        Product product = new Product();
        try
        {
            product = (from p in Context.Products where p.Id == Id select p).First();
            
        }
        catch
        {
        }
        return product;
    }
    public bool InsertProduct(Product pr)
    {
        bool result = false;
        try
        {
            Context.Products.InsertOnSubmit(pr);
            Context.SubmitChanges();
            result = true;
        }
        catch
        {
        }
        return result;
    }
    public bool UpdateStatus(int Id)
    {
        bool result = false;
        try
        {
            Product product = (from p in Context.Products where p.Id == Id select p).First();
            if (product != null)
            {
                product.IsEnabled = !product.IsEnabled;
                Context.SubmitChanges();
                result = true;
            }
        }
        catch
        {
        }
        return result;
    }



    
}